package com.kcsj.controller.basic;


import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.File;
import com.kcsj.service.user.FileService;
import com.kcsj.util.MessageHelper;
import com.kcsj.util.UploadHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet(name="Upload",urlPatterns="/upload")
public class Upload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求字符编码为UTF-8
        request.setCharacterEncoding("UTF-8");
        //设置响应字符编码为UTF-8
        response.setContentType("application/json;charset=UTF-8");
        String path = "D:\\课程设计\\kcsj\\src\\assets\\img\\avatar\\";
        String relativePath = "assets/img/avatar";
        try {
            List<FileItem> fileList = UploadHelper.getFileList(request, 1024 * 1024, 1024 * 1024 * 10,1024*100);
            for(FileItem item : fileList){
                //得到上传的文件名称，
                String filename = item.getName();
                if(filename==null || filename.trim().equals("")){
                    continue;
                }
                File file = new File(filename,relativePath,null);
                int id = FileService.getInstance().addFile(file);
                String saveFileName = UploadHelper.localUpload(fileList, filename, path);
                log.info(filename);
                JSONObject message = MessageHelper.addMessage("上传成功");
                message.put("fileName", "assets/img/avatar/"+saveFileName);
                response.getWriter().print(message);
            }

        } catch (FileUploadException | SQLException e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.addMessage("上传失败");
            response.getWriter().print(message);
        }
    }
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求字符编码为UTF-8
        request.setCharacterEncoding("UTF-8");
        //设置响应字符编码为UTF-8
        response.setContentType("application/json;charset=UTF-8");
        String path = "E:\\Web前端\\kcsj_vue\\src\\assets\\img\\ProductPic";
        String relativePath = "assets/img/ProductPic/";
        try {
            List<FileItem> fileList = UploadHelper.getFileList(request, 1024 * 1024, 1024 * 1024 * 10,1024*100);
            for(FileItem item : fileList){
                //得到上传的文件名称，
                String filename = item.getName();
                if(filename==null || filename.trim().equals("")){
                    continue;
                }
                File file = new File(filename,relativePath,null);
                int id = FileService.getInstance().addFile(file);
                String saveFileName = UploadHelper.localUpload(fileList, filename, path);
                log.info(filename);
                JSONObject message = MessageHelper.addMessage("上传成功");
                message.put("fileName", relativePath+saveFileName);
                response.getWriter().print(message);
            }

        } catch (FileUploadException | SQLException e) {
            e.printStackTrace();
            JSONObject message = MessageHelper.addMessage("上传失败");
            response.getWriter().print(message);
        }
    }
}
